using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace F1RacingGame
{
    public class DemoScreen : IGameScreen
    {
        private List<Car> demoCars;
        private List<Rectangle> demoObstacles;
        private List<Rectangle> demoCheckpoints;
        
        // 演示状态
        private int demoStep = 0;
        private int demoTimer = 0;
        private const int DEMO_STEP_DURATION = 120; // 2秒每步
        private bool isDemoActive = false;
        
        // UI元素
        private Font titleFont;
        private Font infoFont;
        private SolidBrush titleBrush;
        private SolidBrush infoBrush;
        private SolidBrush backgroundBrush;
        
        // 演示文本
        private string[] demoTexts = {
            "欢迎来到F1赛车游戏！",
            "使用方向键控制赛车",
            "WASD键也可以控制",
            "P键暂停游戏",
            "ESC键返回主菜单",
            "享受赛车乐趣！"
        };
        
        public void Initialize()
        {
            // 初始化字体和画笔
            titleFont = new Font("Arial", 24, FontStyle.Bold);
            infoFont = new Font("Arial", 16, FontStyle.Regular);
            titleBrush = new SolidBrush(Color.Yellow);
            infoBrush = new SolidBrush(Color.White);
            backgroundBrush = new SolidBrush(Color.DarkBlue);
            
            // 创建演示赛车
            demoCars = new List<Car>
            {
                new Car("Ferrari", 200, 15, 80, 100),
                new Car("McLaren", 190, 14, 75, 95),
                new Car("Mercedes", 195, 16, 85, 100)
            };
            
            // 设置演示赛车初始位置
            for (int i = 0; i < demoCars.Count; i++)
            {
                demoCars[i].Reset(100, GameEngine.GAME_HEIGHT / 2 + (i - 1) * 80);
            }
            
            // 创建演示障碍物
            CreateDemoObstacles();
            
            // 创建演示检查点
            CreateDemoCheckpoints();
            
            // 开始演示
            isDemoActive = true;
            demoStep = 0;
            demoTimer = 0;
        }
        
        private void CreateDemoObstacles()
        {
            demoObstacles = new List<Rectangle>
            {
                new Rectangle(300, 150, 40, 40),
                new Rectangle(500, 250, 40, 40),
                new Rectangle(700, 350, 40, 40)
            };
        }
        
        private void CreateDemoCheckpoints()
        {
            demoCheckpoints = new List<Rectangle>
            {
                new Rectangle(200, 0, 15, GameEngine.GAME_HEIGHT),
                new Rectangle(400, 0, 15, GameEngine.GAME_HEIGHT),
                new Rectangle(600, 0, 15, GameEngine.GAME_HEIGHT),
                new Rectangle(780, 0, 15, GameEngine.GAME_HEIGHT)
            };
        }
        
        public void Update()
        {
            if (!isDemoActive)
                return;
            
            demoTimer++;
            
            // 更新演示步骤
            if (demoTimer >= DEMO_STEP_DURATION)
            {
                demoStep = (demoStep + 1) % demoTexts.Length;
                demoTimer = 0;
            }
            
            // 更新演示赛车
            UpdateDemoCars();
        }
        
        private void UpdateDemoCars()
        {
            // 演示赛车1 - 直线加速
            demoCars[0].IsAccelerating = true;
            demoCars[0].IsTurningLeft = false;
            demoCars[0].IsTurningRight = false;
            
            // 演示赛车2 - 左右转向
            demoCars[1].IsAccelerating = true;
            if (demoTimer < 60)
            {
                demoCars[1].IsTurningLeft = true;
                demoCars[1].IsTurningRight = false;
            }
            else
            {
                demoCars[1].IsTurningLeft = false;
                demoCars[1].IsTurningRight = true;
            }
            
            // 演示赛车3 - 避障
            demoCars[2].IsAccelerating = true;
            bool shouldAvoid = false;
            foreach (var obstacle in demoObstacles)
            {
                if (demoCars[2].GetBounds().IntersectsWith(obstacle))
                {
                    shouldAvoid = true;
                    break;
                }
            }
            
            if (shouldAvoid)
            {
                demoCars[2].IsTurningLeft = true;
                demoCars[2].IsTurningRight = false;
            }
            else
            {
                demoCars[2].IsTurningLeft = false;
                demoCars[2].IsTurningRight = false;
            }
            
            // 更新所有赛车
            foreach (var car in demoCars)
            {
                car.Update();
            }
        }
        
        public void Render(Graphics graphics)
        {
            // 确保资源已初始化
            if (backgroundBrush == null || titleFont == null || titleBrush == null)
            {
                Initialize();
            }
            
            // 绘制背景
            graphics.FillRectangle(backgroundBrush, 0, 0, GameEngine.GAME_WIDTH, GameEngine.GAME_HEIGHT);
            
            // 绘制赛道边界
            DrawDemoTrack(graphics);
            
            // 绘制障碍物
            DrawDemoObstacles(graphics);
            
            // 绘制检查点
            DrawDemoCheckpoints(graphics);
            
            // 绘制演示赛车
            foreach (var car in demoCars)
            {
                car.Render(graphics);
            }
            
            // 绘制演示信息
            DrawDemoInfo(graphics);
            
            // 绘制提示
            DrawDemoHint(graphics);
        }
        
        private void DrawDemoTrack(Graphics graphics)
        {
            Pen trackPen = new Pen(Color.White, 2);
            graphics.DrawRectangle(trackPen, 50, 50, GameEngine.GAME_WIDTH - 100, GameEngine.GAME_HEIGHT - 100);
            trackPen.Dispose();
        }
        
        private void DrawDemoObstacles(Graphics graphics)
        {
            SolidBrush obstacleBrush = new SolidBrush(Color.Red);
            foreach (var obstacle in demoObstacles)
            {
                graphics.FillRectangle(obstacleBrush, obstacle);
            }
            obstacleBrush.Dispose();
        }
        
        private void DrawDemoCheckpoints(Graphics graphics)
        {
            for (int i = 0; i < demoCheckpoints.Count; i++)
            {
                Color checkpointColor = Color.Gray;
                if (i == (demoTimer / 30) % demoCheckpoints.Count)
                {
                    checkpointColor = Color.Yellow;
                }
                
                SolidBrush checkpointBrush = new SolidBrush(checkpointColor);
                graphics.FillRectangle(checkpointBrush, demoCheckpoints[i]);
                checkpointBrush.Dispose();
            }
        }
        
        private void DrawDemoInfo(Graphics graphics)
        {
            // 绘制标题
            string title = "游戏演示";
            SizeF titleSize = graphics.MeasureString(title, titleFont);
            float titleX = (GameEngine.GAME_WIDTH - titleSize.Width) / 2;
            graphics.DrawString(title, titleFont, titleBrush, titleX, 20);
            
            // 绘制演示文本
            string demoText = demoTexts[demoStep];
            SizeF textSize = graphics.MeasureString(demoText, infoFont);
            float textX = (GameEngine.GAME_WIDTH - textSize.Width) / 2;
            graphics.DrawString(demoText, infoFont, infoBrush, textX, 60);
            
            // 绘制赛车信息
            string carInfo = "演示赛车: Ferrari, McLaren, Mercedes";
            graphics.DrawString(carInfo, infoFont, infoBrush, 20, GameEngine.GAME_HEIGHT - 80);
            
            // 绘制操作说明
            string controls = "操作: 方向键/WASD控制, P暂停, ESC返回";
            graphics.DrawString(controls, infoFont, infoBrush, 20, GameEngine.GAME_HEIGHT - 60);
        }
        
        private void DrawDemoHint(Graphics graphics)
        {
            string hint = "按任意键开始游戏";
            Font hintFont = new Font("Arial", 14, FontStyle.Bold);
            SolidBrush hintBrush = new SolidBrush(Color.Cyan);
            SizeF hintSize = graphics.MeasureString(hint, hintFont);
            float hintX = (GameEngine.GAME_WIDTH - hintSize.Width) / 2;
            graphics.DrawString(hint, hintFont, hintBrush, hintX, GameEngine.GAME_HEIGHT - 30);
            
            hintFont.Dispose();
            hintBrush.Dispose();
        }
        
        public void OnKeyDown(KeyEventArgs e)
        {
            // 任何按键都会开始游戏
            GameManager.Instance.ChangeState(GameState.GamePlay);
        }
        
        public void OnKeyUp(KeyEventArgs e)
        {
            // 演示屏幕不需要处理按键释放
        }
        
        public void Dispose()
        {
            titleFont?.Dispose();
            infoFont?.Dispose();
            titleBrush?.Dispose();
            infoBrush?.Dispose();
            backgroundBrush?.Dispose();
        }
    }
} 